﻿Imports System.Text

Public Class TreeTableFromBase

#Region "类型追加 2009-12-25"
    Dim _typeId As Integer = -1
    Public Property TypeId() As Integer
        Get
            Return _typeId
        End Get
        Set(ByVal value As Integer)
            _typeId = value
        End Set
    End Property

    Dim _rootTitle As String = ""
    Public Property RootTitle() As String
        Get
            Return _rootTitle
        End Get
        Set(ByVal value As String)
            _rootTitle = value
        End Set
    End Property
#End Region

#Region "确定按钮事件"
    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
        If MyTreeView1.SelectedNode IsNot Nothing Then
            If 模式 <> 模式型.查询 AndAlso MyTreeView1.SelectedNode.Nodes.Count > 0 Then
                ShowMessageBox("不能选择含有子节点的")
                Exit Sub
            End If
            Result = MyTreeView1.SelectedNode.Tag.ToString
            DisplayNameValue = MyTreeView1.SelectedNode.Text
            If Result = "根" Then
                Result = ""
                DisplayNameValue = ""
            End If
        End If
        Cancel = False
        Me.Close()
    End Sub
#End Region

#Region "取消按钮事件"
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub
#End Region

#Region "要返回的主键"
    Dim _Id As String = ""
    Public Property ID() As String
        Get
            Return _Id
        End Get
        Set(ByVal value As String)
            _Id = value
        End Set
    End Property
#End Region

#Region "父主键"
    Dim _ParentId As String = ""
    Public Property ParentID() As String
        Get
            Return _ParentId
        End Get
        Set(ByVal value As String)
            _ParentId = value
        End Set
    End Property
#End Region

#Region "树要显示的名称"
    Dim _DisplayName As String = ""
    Public Property DisplayName() As String
        Get
            Return _DisplayName
        End Get
        Set(ByVal value As String)
            _DisplayName = value
        End Set
    End Property
#End Region

#Region "表名"
    Dim _tableName As String = ""
    Public Property TableName() As String
        Get
            Return _tableName
        End Get
        Set(ByVal value As String)
            _tableName = value
        End Set
    End Property
#End Region

#Region "生成的SQL"
    Private Function MakeSql(Optional ByVal strId As String = "") As String
        MakeSql = ""
        Dim SB As New StringBuilder
        SB.Append(" Select ")
        If Not String.IsNullOrEmpty(ID) Then
            SB.Append(" " + ID + " ")
        End If

        If Not String.IsNullOrEmpty(ParentID) Then
            SB.Append("," + ParentID + " ")
        End If

        If Not String.IsNullOrEmpty(DisplayName) Then
            SB.Append("," + DisplayName + " ")
        End If

        SB.Append(" From " + TableName + " ")

        SB.Append(" Where  " + ParentID)

        If Not String.IsNullOrEmpty(strId) Then
            SB.Append("=" + """")
            SB.Append(strId)
            SB.Append("""")
        Else
            SB.Append(" is null ")
        End If

        '2009-12-25
        If _typeId <> -1 Then
            SB.Append(" And Type='" + Me.TypeId.ToString() + "' ")
        End If
        '2009-12-25
        Return SB.ToString
    End Function
#End Region

#Region "初始化选择的主键"
    Dim _PrimateKey As String = ""
    Public Property PrimateKey() As String
        Get
            Return _PrimateKey
        End Get
        Set(ByVal value As String)
            _PrimateKey = value
        End Set
    End Property
#End Region


#Region "窗体初始化事件"
    Private Sub TreeTableFromBase_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim nd As TreeNode = Me.MyTreeView1.Nodes.Add("根")
        nd.Tag = "根"
        LoadData(nd, IdParent)
        'Me.MyTreeView1.ExpandAll() 'D-2009-12-28 去掉提高性能
        'A-2009-12-28
        If nd IsNot Nothing Then
            nd.Expand()
        End If
        'A-2009-12-28
        If _SelectNode IsNot Nothing Then
            Me.MyTreeView1.SelectedNode = _SelectNode
        End If

        If Me.是否关联 = True Then
            Me.ControlBox = False
            Me.btnCancel.Visible = False
            Me.btnOk.Visible = False
        End If
        Me.ImageList1.Images.Add(My.Resources.打开)
        Me.ImageList1.Images.Add(My.Resources.文件夹)
        Me.MyTreeView1.ImageList = ImageList1
    End Sub

    Private _idParent As String = ""
    Public Property IdParent() As String
        Get
            Return _idParent
        End Get
        Set(ByVal value As String)
            _idParent = value
        End Set
    End Property

    'A-2010-1-2
    Private _ExpandChild As Boolean = True
    Public Property ExpandChild() As Boolean
        Get
            Return _ExpandChild
        End Get
        Set(ByVal value As Boolean)
            _ExpandChild = value
        End Set
    End Property
    'A-2010-1-2

    Private Sub LoadData(Optional ByVal tdParent As TreeNode = Nothing, Optional ByVal idParent As String = "")

        Dim rs As ADODB.Recordset = ct.GetTable(MakeSql(idParent))

        If rs Is Nothing Then Exit Sub

        If rs.EOF Then
            GoTo end1
        End If

        While Not rs.EOF
            Dim td As TreeNode = Nothing
            Dim strTemp As String = ""

            If Not String.IsNullOrEmpty(rs.Fields(DisplayName).Value.ToString()) Then
                strTemp = rs.Fields(DisplayName).Value.ToString()
            End If

            If tdParent IsNot Nothing Then
                td = tdParent.Nodes.Add(strTemp)
            Else
                td = Me.MyTreeView1.Nodes.Add(strTemp)
            End If
            td.ImageIndex = 1
            td.SelectedImageIndex = 0
            strTemp = ""

            If Not String.IsNullOrEmpty(rs.Fields(ID).Value.ToString()) Then
                strTemp = rs.Fields(ID).Value.ToString()
            End If

            td.Tag = strTemp

            If (strTemp = PrimateKey) Then
                'td.Checked = True
                _SelectNode = td
            End If

            strTemp = ""

            If Not String.IsNullOrEmpty(rs.Fields(ID).Value.ToString()) Then
                strTemp = rs.Fields(ID).Value.ToString()
            End If

            'A-2010-1-2
            If ExpandChild Then
                If Not String.IsNullOrEmpty(strTemp) Then
                    LoadData(td, strTemp)
                End If
            End If
            'A-2010-1-2

            rs.MoveNext()
        End While
end1:
        rs.Close()
        System.Runtime.InteropServices.Marshal.ReleaseComObject(rs) '20100531
        rs = Nothing
    End Sub
#End Region

#Region "返回的结果"
    Dim _Result As String = ""
    Public Property Result() As String
        Get
            Return _Result
        End Get
        Set(ByVal value As String)
            _Result = value
        End Set
    End Property

    Dim _DisplayNameValue As String = ""
    Public Property DisplayNameValue() As String
        Get
            Return _DisplayNameValue
        End Get
        Set(ByVal value As String)
            _DisplayNameValue = value
        End Set
    End Property

    Dim _cancel As Boolean = True
    Public Property Cancel() As Boolean
        Get
            Return _cancel
        End Get
        Set(ByVal value As Boolean)
            _cancel = value
        End Set
    End Property
#End Region

    Private _关联 As Boolean = False
    Public Property 是否关联() As Boolean
        Get
            Return _关联
        End Get
        Set(ByVal value As Boolean)
            _关联 = value
            Me.MyTreeView1.Dock = DockStyle.Fill
        End Set
    End Property

    Public Enum 模式型
        添加
        修正
        查询
    End Enum

    Private _模式 As 模式型 = 模式型.查询
    Public Property 模式() As 模式型
        Get
            Return _模式
        End Get
        Set(ByVal value As 模式型)
            _模式 = value
        End Set
    End Property


    Public Sub New()
        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()
        ' 在 InitializeComponent() 调用之后添加任何初始化。
    End Sub

    Dim _SelectNode As TreeNode = Nothing
    Public Sub New(ByVal strId As String)

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。
        PrimateKey = strId
    End Sub

    Public Event SelectData(ByVal result As String, ByVal DisplayNameValue As String)

    Private Sub MyTreeView1_NodeMouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles MyTreeView1.NodeMouseDoubleClick
        If MyTreeView1.SelectedNode IsNot Nothing Then
            If MyTreeView1.SelectedNode.Nodes.Count > 0 AndAlso 是否关联 = False Then
                Exit Sub
            End If
            Result = MyTreeView1.SelectedNode.Tag.ToString
            DisplayNameValue = MyTreeView1.SelectedNode.Text
            If Result = "根" Then
                Result = ""
                DisplayNameValue = ""
            End If
        End If
        Cancel = False
        If Not 是否关联 Then
            Me.Close()
        Else
            RaiseEvent SelectData(Result, DisplayNameValue)
        End If
    End Sub
End Class
